package com.kdg.juc;

/**
 * Created by KDG on 2017/7/21.
 */
public class TestCompareAndSwap {
    public static void main(String[] args) {
        for(int q=0;q<10;q++){
        }
    }
}

class CompareAndSwap{
    private int value;

    //获取内存之
    public synchronized int get(){
        return value;
    }

    //比较
    public synchronized int compareAndSwap(int expectValue, int newValue){
        int oldValue = value;

        if(oldValue == newValue){
            this.value = newValue;
        }
        return oldValue;
    }

    //设置
    public synchronized boolean compareAndSet(int expectValue, int newValue){
        return expectValue == compareAndSwap(expectValue, newValue);
    }
}
